package com.sunwayworld.basemodule.coredata.judgmentbasis.service.impl;

import com.sunwayworld.basemodule.coredata.judgmentbasis.bean.LimsJudgmentBasisBean;
import com.sunwayworld.basemodule.coredata.judgmentbasis.bean.LimsJudgmentBasisTestmethodBean;
import com.sunwayworld.basemodule.coredata.judgmentbasis.bean.LimsJudgmentBasisTmAnalyteBean;
import com.sunwayworld.basemodule.coredata.judgmentbasis.bean.LimsJudgmentBasisTmAnalyteHighlowBean;
import com.sunwayworld.basemodule.coredata.judgmentbasis.dao.LimsJudgmentBasisDao;
import com.sunwayworld.basemodule.coredata.judgmentbasis.service.LimsJudgmentBasisService;
import com.sunwayworld.basemodule.coredata.judgmentbasis.service.LimsJudgmentBasisTestmethodService;
import com.sunwayworld.basemodule.coredata.judgmentbasis.service.LimsJudgmentBasisTmAnalyteHighlowService;
import com.sunwayworld.basemodule.coredata.judgmentbasis.service.LimsJudgmentBasisTmAnalyteService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Repository
@GikamBean
public class LimsJudgmentBasisServiceImpl implements LimsJudgmentBasisService {

    @Autowired
    private LimsJudgmentBasisDao limsJudgmentBasisDao;

    @Autowired
    @Lazy
    private LimsJudgmentBasisTestmethodService testmethodService;

    @Autowired
    @Lazy
    private LimsJudgmentBasisTmAnalyteService analyteService;

    @Autowired
    @Lazy
    private LimsJudgmentBasisTmAnalyteHighlowService highlowService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsJudgmentBasisDao getDao() {
        return limsJudgmentBasisDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsJudgmentBasisBean limsJudgmentBasis = jsonWrapper.parseUnique(LimsJudgmentBasisBean.class);
        limsJudgmentBasis.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsJudgmentBasis);
        return limsJudgmentBasis.getId();
    }

    @Override
    @Transactional
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsJudgmentBasisBean> parse = wrapper.parse(LimsJudgmentBasisBean.class);
        if(CollectionUtils.isEmpty(parse)) return;
        // 级联删除
        List<LimsJudgmentBasisTestmethodBean> testMethodBeans = new ArrayList<>();
        List<LimsJudgmentBasisTmAnalyteBean> tmAnalyteBeans = new ArrayList<>();
        List<LimsJudgmentBasisTmAnalyteHighlowBean> tmAnalyteHighlowBeans = new ArrayList<>();
        parse.forEach(e -> {
            LimsJudgmentBasisTestmethodBean testMethodBean = new LimsJudgmentBasisTestmethodBean();
            testMethodBean.setJudgId(e.getId());
            testMethodBeans.add(testMethodBean);
            LimsJudgmentBasisTmAnalyteBean analyteBean = new LimsJudgmentBasisTmAnalyteBean();
            analyteBean.setJudgId(e.getId());
            tmAnalyteBeans.add(analyteBean);
            LimsJudgmentBasisTmAnalyteHighlowBean highlowBean = new LimsJudgmentBasisTmAnalyteHighlowBean();
            highlowBean.setJudgId(e.getId());
            tmAnalyteHighlowBeans.add(highlowBean);
        });
        testmethodService.getDao().deleteBy(testMethodBeans, "judgId");
        analyteService.getDao().deleteBy(tmAnalyteBeans, "judgId");
        highlowService.getDao().deleteBy(tmAnalyteHighlowBeans, "judgId");
        getDao().deleteBy(parse);
    }
}
